-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[type] [refactor] Add compute_type for CustomIntType #2047
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2047 +/- ##
==========================================
+ Coverage 43.54% 43.56% +0.01%
==========================================
Files 45 45
Lines 6267 6267
Branches 1110 1110
==========================================
+ Hits 2729 2730 +1
Misses 3366 3366
+ Partials 172 171 -1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
taichi/ir/type.h
Outdated
// and should we expose it to users? | ||
TI_ASSERT(num_bits <= 32); | ||
compute_type = is_signed ? new PrimitiveType(PrimitiveTypeID::i32) | ||
: new PrimitiveType(PrimitiveTypeID::u32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use type_factory
here?
Please don't merge this pr for now. I have found some potential problems and will request your review after I fixed them. Thanks! |
taichi/codegen/codegen_llvm.cpp
Outdated
auto left = | ||
builder->CreateSub(tlctx->get_constant(compute_type_size), bit_end); | ||
auto right = builder->CreateSub(tlctx->get_constant(compute_type_size), | ||
tlctx->get_constant(cit->get_num_bits())); | ||
left = builder->CreateIntCast(left, llvm_type(cit->get_compute_type()), | ||
cit->get_is_signed()); | ||
right = builder->CreateIntCast(right, llvm_type(cit->get_compute_type()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After testing the case of 16-bits and 64-bits, I found that we should use the num of physical_type
bits in BitStructType
SNode here instead of the compute_type
of CustomIntType
. So I decided to convert this pr to draft and reimplement this part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I didn't go through every file but it looks that we are on the right track!
taichi/codegen/codegen_llvm.cpp
Outdated
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) { | ||
if (dt->is_primitive(PrimitiveTypeID::i8) || | ||
dt->is_primitive(PrimitiveTypeID::u8)) { | ||
return llvm::Type::getInt8PtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::i16) || | ||
dt->is_primitive(PrimitiveTypeID::u16)) { | ||
return llvm::Type::getInt16PtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::i32) || | ||
dt->is_primitive(PrimitiveTypeID::u32)) { | ||
return llvm::Type::getInt32PtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::i64) || | ||
dt->is_primitive(PrimitiveTypeID::u64)) { | ||
return llvm::Type::getInt64PtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::u1)) { | ||
return llvm::Type::getInt1PtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::f32)) { | ||
return llvm::Type::getFloatPtrTy(*llvm_context); | ||
} else if (dt->is_primitive(PrimitiveTypeID::f64)) { | ||
return llvm::Type::getDoublePtrTy(*llvm_context); | ||
} else { | ||
TI_NOT_IMPLEMENTED; | ||
} | ||
return nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) { | |
if (dt->is_primitive(PrimitiveTypeID::i8) || | |
dt->is_primitive(PrimitiveTypeID::u8)) { | |
return llvm::Type::getInt8PtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::i16) || | |
dt->is_primitive(PrimitiveTypeID::u16)) { | |
return llvm::Type::getInt16PtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::i32) || | |
dt->is_primitive(PrimitiveTypeID::u32)) { | |
return llvm::Type::getInt32PtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::i64) || | |
dt->is_primitive(PrimitiveTypeID::u64)) { | |
return llvm::Type::getInt64PtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::u1)) { | |
return llvm::Type::getInt1PtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::f32)) { | |
return llvm::Type::getFloatPtrTy(*llvm_context); | |
} else if (dt->is_primitive(PrimitiveTypeID::f64)) { | |
return llvm::Type::getDoublePtrTy(*llvm_context); | |
} else { | |
TI_NOT_IMPLEMENTED; | |
} | |
return nullptr; | |
} | |
llvm::Type *CodeGenLLVM::llvm_ptr_type(DataType dt) { | |
return llvm::PointerType::get(llvm_type(dt), 0); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally, I think it's time for a formal review. Please help me to check my refactoring! Thanks a lot !
taichi/ir/type_factory.h
Outdated
Type *get_custom_int_type_with_compute_type(int compute_type_bits, | ||
int num_bits, | ||
bool is_signed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function name might be too long. Any good advices would be appreciated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents: actually every CustomIntType
has a compute type. I would use get_custom_int_type(int num_bits, bool is_signed, Type *compute_type=nullptr)
if that makes sense to you. Then two APIs can be merged into one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! Feel free to merge after the comments are addressed. Thanks!
taichi/ir/type.h
Outdated
CustomIntType(int num_bits, bool is_signed); | ||
|
||
CustomIntType(int compute_type_bits, int numBits, bool isSigned); | ||
|
||
CustomIntType(int compute_type_bits, | ||
Type *physical_type, | ||
int num_bits, | ||
bool is_signed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A single ctor CustomIntType(int num_bits, bool is_signed, Type *physical_type_=nullptr, Type *compute_type_=nullptr)
may be cleaner?
taichi/ir/type_factory.h
Outdated
Type *get_custom_int_type_with_compute_type(int compute_type_bits, | ||
int num_bits, | ||
bool is_signed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents: actually every CustomIntType
has a compute type. I would use get_custom_int_type(int num_bits, bool is_signed, Type *compute_type=nullptr)
if that makes sense to you. Then two APIs can be merged into one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Just one minor comment improvement
taichi/codegen/codegen_llvm.cpp
Outdated
@@ -328,7 +328,8 @@ void CodeGenLLVM::visit(UnaryOpStmt *stmt) { | |||
auto from_size = 0; | |||
if (from->is<CustomIntType>()) { | |||
// TODO: replace 32 with a customizable type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove TODO here
Related issue = #1905
In this pr, we add an attribute for
CustomIntType
named 'compute_type' and replace some 'hand-write 32'. SomeTODO
could be removed after this pr being merged.[Click here for the format server]